#!/bin/sh

set -x

# shellcheck disable=SC2046,SC2035
codespell -w --ignore-words ci/codespell.ignores --builtin clear,rare,informal,names,en-GB_to_en-US --skip *.png,*.PNG,*.pyc,src/rdb/raft/*,src/control/vendor/*,RSA.golden,utils/rpms/* $(git diff --name-only origin/"$CHANGE_TARGET"...)

# The return code of codespell is the number of words it could not correct
# because of multiple options.  We could report on these but they're rare
# so just save the exit code here and use it later on.  They are reported
# in the output of the CI job, just will not be commented on in github.
# CORCI-988 tracks this, with a view to improving reporting in this case.
RC=$?

def_ignore="SPLIT_STRING,SSCANF_TO_KSTRTO,PREFER_KERNEL_TYPES"
def_ignore+=",USE_NEGATIVE_ERRNO,CAMELCASE,STATIC_CONST_CHAR_ARRAY"
def_ignore+=",COMPARISON_TO_NULL,COMPLEX_MACRO,BIT_MACRO"
def_ignore+=",CONCATENATED_STRING"

CP=../code_review/checkpatch.pl
if [ ! -x $CP ]
then
	CP=./code_review/checkpatch.pl
fi

if [ -x $CP ]
then
    for FILE in $(git diff --name-only origin/"$CHANGE_TARGET"... | grep "^src" | grep -v -e src/control/vendor -e .go\$ -e pb-c -e debug_setup.h)
    do
	ignore="$def_ignore"
	if grep -lq CRT_RPC_DECLARE "$FILE"; then
	    ignore+=",SPACING_CAST"
	fi
        $CP --quiet --no-tree --file --show-types --no-summary --ignore "$ignore" --strict "$FILE" --fix-inplace
    done
fi

exit $RC
